package com.mifengs.mall.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mifengs.mall.R;
import com.mifengs.mall.entity.AdvertBean;
import com.mifengs.mall.entity.FloorBean;
import com.mifengs.mall.util.ToIntentUtil;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
import com.yqritc.recyclerviewflexibledivider.VerticalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lihaoqi on 2017/10/31.
 */

public class FourGridFloAdapter extends  DelegateAdapter.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private FourGridtemAdapter mFourGridtemAdapter;
    private FloorBean floorBean;
    public FourGridFloAdapter(Context mContext) {
        this.mContext = mContext;
    }
    public void setFloorData(FloorBean floorBean){
        this.floorBean = floorBean;
        notifyDataSetChanged();
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return  new LinearLayoutHelper();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new FourGridViewHolder(LayoutInflater.from(mContext).inflate(R.layout.fragment_four_flo,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof FourGridViewHolder){
            FourGridViewHolder viewHolder = (FourGridViewHolder)holder;
            final RecyclerView mRecyclerView = viewHolder.mRecyclerView;
            if(floorBean !=null){
                final List<AdvertBean> advertBeanList = floorBean.getAdverts();
                mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,4));
                mRecyclerView.setNestedScrollingEnabled(false);
                mFourGridtemAdapter= new FourGridtemAdapter(R.layout.item_four_floor,advertBeanList,mContext);
                mRecyclerView.setAdapter(mFourGridtemAdapter);
                mFourGridtemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        AdvertBean adverts = advertBeanList.get(position);
                        ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                    }
                });
            }

        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }


    public class FourGridViewHolder extends RecyclerView.ViewHolder{

        RecyclerView mRecyclerView;
        public FourGridViewHolder(View itemView) {
            super(itemView);
            mRecyclerView = (RecyclerView)itemView.findViewById(R.id.four_grid_list);

        }
    }
}
